////////////////////////////////////////////////
// MACHINE GENERATED, PLEASE DONOT MODIFY!    //
////////////////////////////////////////////////

using System;
using Google.Protobuf;
using System.IO;
using System.Net;

namespace OutOfUnity
{
    public class ProtocolFinder
    {
        private const int RECV_BUF_SIZE = 1024 * 1024;
        private byte[] sendBuffer_ = new byte[RECV_BUF_SIZE];
        private MemoryStream sendStream_ = new MemoryStream(RECV_BUF_SIZE);
        private NetworkMethod method_ = null;
        public ProtocolFinder(NetworkMethod method)
        {
            method_ = method;
        }
        #region PublicMethod
        public byte[] serialize(uint msgID, object o, out int length)
        {
            var pbo = o as IMessage;
            var pbSize = pbo.CalculateSize();
            var size = 8 + pbSize;
            var byteArray = new byte[8 + pbSize];
            var orderedMsgID = IPAddress.HostToNetworkOrder((int)msgID);
            var orderedLength = IPAddress.HostToNetworkOrder((int)size);
            MemoryStream stream = new MemoryStream(8 + pbSize);
            if (pbSize > 0)
            {
                stream.Position = 8;
                pbo.WriteTo(stream);
            }
            stream.Seek(0, SeekOrigin.Begin);
            var lengthBytes = BitConverter.GetBytes(orderedLength);
            var msgIDBytes = BitConverter.GetBytes(orderedMsgID);
            stream.Write(lengthBytes, 0, lengthBytes.Length);
            stream.Write(msgIDBytes, 0, msgIDBytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(byteArray, 0, size);
            stream.Close();
            length = size;
            return byteArray;
        }
        public object deserialize(byte[] byteArray, int length, uint msgID)
        {
            var pbo = getMessage(msgID);
            pbo.MergeFrom(byteArray, 0, length);
            return pbo;
        }
                public bool send(DummyEntityAck msg)
        {
             return method_.send((int)ProtocolID.DUMMYENTITYACK, msg);
        }
        public bool send(LoginAck msg)
        {
             return method_.send((int)ProtocolID.LOGINACK, msg);
        }
        public bool send(EchoAck msg)
        {
             return method_.send((int)ProtocolID.ECHOACK, msg);
        }
        public bool send(EnterAck msg)
        {
             return method_.send((int)ProtocolID.ENTERACK, msg);
        }
        public bool send(EnterNtf msg)
        {
             return method_.send((int)ProtocolID.ENTERNTF, msg);
        }
        public bool send(LeaveNtf msg)
        {
             return method_.send((int)ProtocolID.LEAVENTF, msg);
        }
        public bool send(MoveNtf msg)
        {
             return method_.send((int)ProtocolID.MOVENTF, msg);
        }
        public bool send(PingAck msg)
        {
             return method_.send((int)ProtocolID.PINGACK, msg);
        }
        public bool send(SceneEnterReq msg)
        {
             return method_.send((int)ProtocolID.SCENEENTERREQ, msg);
        }
        public bool send(SceneLeaveReq msg)
        {
             return method_.send((int)ProtocolID.SCENELEAVEREQ, msg);
        }
        public bool send(SceneLeaveNtf msg)
        {
             return method_.send((int)ProtocolID.SCENELEAVENTF, msg);
        }
        public bool send(SceneEnterNtf msg)
        {
             return method_.send((int)ProtocolID.SCENEENTERNTF, msg);
        }
        public bool send(SceneMoveNtf msg)
        {
             return method_.send((int)ProtocolID.SCENEMOVENTF, msg);
        }
        public bool send(LoginReq msg)
        {
             return method_.send((int)ProtocolID.LOGINREQ, msg);
        }
        public bool send(EchoReq msg)
        {
             return method_.send((int)ProtocolID.ECHOREQ, msg);
        }
        public bool send(EnterReq msg)
        {
             return method_.send((int)ProtocolID.ENTERREQ, msg);
        }
        public bool send(AddBuffNtf msg)
        {
             return method_.send((int)ProtocolID.ADDBUFFNTF, msg);
        }
        public bool send(SceneEnterAck msg)
        {
             return method_.send((int)ProtocolID.SCENEENTERACK, msg);
        }
        public bool send(SceneLeaveAck msg)
        {
             return method_.send((int)ProtocolID.SCENELEAVEACK, msg);
        }
        public bool send(RoleEnterNtf msg)
        {
             return method_.send((int)ProtocolID.ROLEENTERNTF, msg);
        }
        public bool send(RoleLeaveNtf msg)
        {
             return method_.send((int)ProtocolID.ROLELEAVENTF, msg);
        }
        public bool send(RoleMoveNtf msg)
        {
             return method_.send((int)ProtocolID.ROLEMOVENTF, msg);
        }
        public bool send(ClientMoveNtf msg)
        {
             return method_.send((int)ProtocolID.CLIENTMOVENTF, msg);
        }
        public bool send(PingReq msg)
        {
             return method_.send((int)ProtocolID.PINGREQ, msg);
        }
        public bool send(DummyReq msg)
        {
             return method_.send((int)ProtocolID.DUMMYREQ, msg);
        }
        public bool send(DummyEntityReq msg)
        {
             return method_.send((int)ProtocolID.DUMMYENTITYREQ, msg);
        }

        #endregion
        #region PrivateMethod
        IMessage getMessage(uint msgID)
        {
            switch (msgID)
            {
                            case ProtocolID.DUMMYENTITYACK:
                return new DummyEntityAck();
            case ProtocolID.LOGINACK:
                return new LoginAck();
            case ProtocolID.ECHOACK:
                return new EchoAck();
            case ProtocolID.ENTERACK:
                return new EnterAck();
            case ProtocolID.ENTERNTF:
                return new EnterNtf();
            case ProtocolID.LEAVENTF:
                return new LeaveNtf();
            case ProtocolID.MOVENTF:
                return new MoveNtf();
            case ProtocolID.PINGACK:
                return new PingAck();
            case ProtocolID.SCENEENTERREQ:
                return new SceneEnterReq();
            case ProtocolID.SCENELEAVEREQ:
                return new SceneLeaveReq();
            case ProtocolID.SCENELEAVENTF:
                return new SceneLeaveNtf();
            case ProtocolID.SCENEENTERNTF:
                return new SceneEnterNtf();
            case ProtocolID.SCENEMOVENTF:
                return new SceneMoveNtf();
            case ProtocolID.LOGINREQ:
                return new LoginReq();
            case ProtocolID.ECHOREQ:
                return new EchoReq();
            case ProtocolID.ENTERREQ:
                return new EnterReq();
            case ProtocolID.ADDBUFFNTF:
                return new AddBuffNtf();
            case ProtocolID.SCENEENTERACK:
                return new SceneEnterAck();
            case ProtocolID.SCENELEAVEACK:
                return new SceneLeaveAck();
            case ProtocolID.ROLEENTERNTF:
                return new RoleEnterNtf();
            case ProtocolID.ROLELEAVENTF:
                return new RoleLeaveNtf();
            case ProtocolID.ROLEMOVENTF:
                return new RoleMoveNtf();
            case ProtocolID.CLIENTMOVENTF:
                return new ClientMoveNtf();
            case ProtocolID.PINGREQ:
                return new PingReq();
            case ProtocolID.DUMMYREQ:
                return new DummyReq();
            case ProtocolID.DUMMYENTITYREQ:
                return new DummyEntityReq();

            }
            return null;
        }        
        #endregion
    }
}

